package magicboard.model
{
  import flash.display.Graphics;
  import flash.geom.Point;
  
	
	/**
	 * Curve considit of sets of points 
	 */
	public class Polyline implements GeometryObject
	{
	  // --------------------------------------------------------------------------------------------
		public function Polyline(ps:Array = null){
			
			if(ps != null){
		  	_points = ps;
		 	}else{
				_points = new Array();
		 	}
		}
		
	  // --------------------------------------------------------------------------------------------
		public function get points():Array {return _points;}
		
		
	  // --------------------------------------------------------------------------------------------
		public function draw(g:Graphics):void{

    	g.moveTo(points[0].x, points[0].y);
    	for(var i:int = 1; i < points.length; i++) {
    		g.lineTo(points[i].x, points[i].y);
    	}
		}
		
	  // --------------------------------------------------------------------------------------------
		public function getLineLength():Number{
      var length:Number = 0;
      var v:Vector;

    	for(var i:int = 1; i < points.length; i++) {
    	  v = Vector.points2Vector(points[i], points[i-1]);
    		length += v.getLength();
    	}
    	
		  return length;
		}

	  // --------------------------------------------------------------------------------------------
		public function toString():String{
		  return "Polyline with " + (points.length-1) + " line segments.";
		}

	  // --------------------------------------------------------------------------------------------
		public function saveToString():String{
			var text:String = "polyline: ";
			
			for(var i:int = 0; i < _points.length; i++){
				text += "(" + _points[i].x + "," + _points[i].y + ")";
				if( i < _points.length-1)
					text += ", "; 
			}
			
			return text;
		}

	  // --------------------------------------------------------------------------------------------
		public function getType():String{
			return "polyline";
		}

	  // --------------------------------------------------------------------------------------------
		public function addPoint(p:Point):void{
		  points.push(p);
		}
		
		
	  // --------------------------------------------------------------------------------------------
	  /**
	  * Convert curve to the line segment.
	  */ 
		public function toLineSegment():LineSegment{
		  var segment:LineSegment = new LineSegment(points[0], points[points.length-1])
		  return segment;
		}

 	  // --------------------------------------------------------------------------------------------
    // Private members
    private var _points : Array;		
	}
}